﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace VidoopSecure
{
    public class VoicePinService : Service
    {
        /// <summary>
        /// Creates an instance of <see cref="VidoopSecure.VoicePinService" />.
        /// </summary>
        /// <param name="manager">The <see cref="VidoopSecure.VSManager" /> 
        /// for performing operations.</param>
        /// <param name="site">The <see cref="VidoopSecure.Site" /> associated
        /// with the service.</param>
        /// <param name="apiBase">The base URL for the VidoopSecure web service.</param>
        public VoicePinService(VSManager manager, Site site, string apiBase)
            : base(manager, site, apiBase)
        {
        }


        /// <summary>
        /// Creates an instance of <see cref="VidoopSecure.VoicePin" />.
        /// </summary>
        /// <param name="userId">User identifiier.</param>
        /// <param name="phoneNumber">Phone number to which call will be made.</param>
        /// <param name="pin">PIN to be validated in the phone call.</param>
        /// <returns>An instance of <see cref="VidoopSecure.VoicePin" />.</returns>
        public VoicePin NewVoicePin(string userId, string phoneNumber, string pin)
        {
            string apiUrl = this.CreateRequestUrl("/customers/" + this.Site.CustomerId + "/sites/" + this.Site.SiteName + "/services/voicepin");

            string credentials = this.Site.Username + Util.CREDENTIALS_SEPARATOR + this.Site.Password;

            Dictionary<string, string> parameters = new Dictionary<string, string>
            {
                { "user_id", userId },
                { "phone", phoneNumber },
                { "pin", pin }
            };
            
            HttpProxy proxy = this.Manager.ApiCall(apiUrl, "POST", parameters, credentials);

            if (proxy.StatusCode != (int)HttpStatus.Created)
                throw new VidoopSecureException(proxy.StatusCode, "Failed to create a new Voicepin service.");
            else
            {
                Stream stream = proxy.Response.GetResponseStream();

                VoicePin voicepin = VoicePin.FromXml(stream);
                proxy.Close();
                return voicepin;
            }
        }

        /// <summary>
        /// Gets a <see cref="VidoopSecure.VoicePin" /> given an Id.
        /// </summary>
        /// <param name="voicePinId">The identifier of a 
        /// <see cref="VidoopSecure.VoicePin" />.</param>
        /// <returns>An instance of <see cref="VidoopSecure.VoicePin" />.</returns>
        public VoicePin GetVoicePin(string voicePinId)
        {
            string apiUrl = this.CreateRequestUrl("/customers/" + this.Site.CustomerId +
                "/sites/" + this.Site.SiteName + "/services/voicepin/" + voicePinId);

            string credentials = this.Site.Username + Util.CREDENTIALS_SEPARATOR + this.Site.Password;

            HttpProxy proxy = this.Manager.ApiCall(apiUrl, "GET", null, credentials);
            if (proxy.StatusCode != (int)HttpStatus.OK)
            {
                int status = proxy.StatusCode;
                proxy.Close();
                throw new VidoopSecureException(status,
                    string.Format("Failed to get voicepin transaction with Id = {0}", voicePinId));
            }
            else
            {
                VoicePin voicePin = VoicePin.FromXml(proxy.Response.GetResponseStream());
                proxy.Close();
                return voicePin;
            }
        }

    }
}
